package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.UserMapper;
import com.px.model.UserInfo;
import com.px.model.getData;
import com.px.unit.Response;

//把当前类注解为控制器
@Controller
public class UserController {

	// 实例化我们的数据操作类
	UserMapper user_mapper = new UserMapper();

	/**
	 * 登录请求方法
	 * */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/validateUserLogin", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response validateUserLogin(@RequestBody UserInfo user) {
		UserInfo userInfo = user_mapper.validateUserLogin(user);
		getData mydata = new getData();
		mydata.setCurrentUserInfo(userInfo);
		if (userInfo != null) {
			// 登录成功
			return new Response(200, "登录成功", mydata);
		} else {
			// 登录失败
			return new Response(403, "登录失败");
		}
	}

	/**
	 * 获取所有用户信息
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllUsers", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllUsers() {
		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		List<UserInfo> userInfos = user_mapper.getAllUsers();
		mydata.setUserInfolist(userInfos);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 获取指定Id用户信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getUserByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getUserByUserId(@RequestBody UserInfo user) {
		if (user != null) {
			// 判断查询的用户是否存在
			int checkid = user_mapper.checkUserIdIsExist(user);
			if (!(checkid > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<UserInfo> userInfos = user_mapper.getUserByUserId(user);
				mydata.setUserInfolist(userInfos);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	/**
	 * 添加用户信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/addUserInfo", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response addUserInfo(@RequestBody UserInfo user) {

		if (user != null) {

			// 判断添加的用户账户不能重复
			int checkname = user_mapper.checkUserNameIsExist(user);
			if (checkname > 0) {
				return new Response(304, "已存在同名帐号");
			} else {
				int counts = user_mapper.insertUser(user);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 根据用户名删除用户信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteUserByUserName", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteUserByUserName(@RequestBody UserInfo user) {

		if (user != null) {

			// 判断删除的用户是否存在
			int checkname = user_mapper.checkUserNameIsExist(user);
			if (!(checkname > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				int counts = user_mapper.deleteUserByUserName(user);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据用户ID删除用户信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteUserByUserId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteUserByUserId(@RequestBody UserInfo user) {

		if (user != null) {

			// 判断删除的用户是否存在
			int checkid = user_mapper.checkUserIdIsExist(user);
			if (!(checkid > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				int counts = user_mapper.deleteUserByUserId(user);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据用户名修改用户信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateUserByUserName", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateUserByUserName(@RequestBody UserInfo user) {

		if (user != null) {

			// 判断修改的用户是否存在
			int checkname = user_mapper.checkUserNameIsExist(user);
			if (!(checkname > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				int counts = user_mapper.updateUserByUserName(user);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

	/**
	 * 根据用户ID修改用户信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateUserByUserId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateUserByUserId(@RequestBody UserInfo user) {

		if (user != null) {

			// 判断修改的用户是否存在
			int checkid = user_mapper.checkUserIdIsExist(user);
			if (!(checkid > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				int counts = user_mapper.updateUserByUserId(user);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}
}
